<template>
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="show"
      width="50%"
      @close="onCancel"
      :close-on-click-modal="false"
      append-to-body
    >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
    <el-form-item label="导入CSV文件" prop="questionText">
        <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="headers"
        :action="url"
        :data="{repeat:formData.repeat,taskId,outof:formData.outof,black:formData.black}"
        :disabled="isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="updateSupport" />是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      </el-form-item>

      <el-form-item label="乱序导入" prop="outof">
        <el-select
          style="width: 80%"
          v-model="formData.outof"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in list"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="过滤重复号码" prop="repeat">
            <el-select
          style="width: 80%"
          v-model="formData.repeat"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in list"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="过滤黑名单" prop="black">
            <el-select
          style="width: 80%"
          v-model="formData.black"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in list"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
        </el-form-item>

    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="onCancel">取 消</el-button>
      </div>
    </el-dialog>
  </template>
  
  <script>
  
  import { getToken } from "@/utils/auth";
  export default {
    props: {
      importShow: {
        type: Boolean,
        default: false,
      },
      uploadUrl: {
        type: String,
        default: "",
      },
      taskId:{
        type: String,
        default: "",
      },
      downloadUrl: {
        type: String,
        default: "",
      },
    },
    data() {
      return {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        title: "",
        isUploading: false,
        show: this.importShow,
        formData:{},
        rules:{},
        list:[
            {dictValue:'启用',dictLabel:'启用'},
            {dictValue:'禁用',dictLabel:'禁用'}
        ]
      };
    },
    computed: {
      url: function () {
        return process.env.VUE_APP_BASE_API + this.uploadUrl;
      },
    },
    methods: {
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "用户导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download(
          this.downloadUrl,
          {id:this.taskId||''},
          `template_${new Date().getTime()}.xlsx`
        );
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.isUploading = false;
        this.$refs.upload.clearFiles();
        this.onCancel();
        this.$alert(
          "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
            response.msg +
            "</div>",
          "导入结果",
          { dangerouslyUseHTMLString: true }
        );
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
  
      // 取消
      onCancel() {
        this.$emit("onImportCancel");
      },
    },
  };
  </script>
  
  <style scoped></style>
  